<template>
  <div class="base-form">
    <div class="header">
      <slot name="header" />
    </div>
    <el-form
      ref="elForm"
      :label-width="labelWidth"
      :model="formData"
      label-suffix=":"
    >
      <el-row>
        <template v-for="(item, index) in formItems">
          <el-col
            v-show="index < defaultItemsNum || advanced || alwaysShow"
            :key="index"
            v-bind="colLayout"
          >
            <el-form-item
              :label="item.label"
              :rules="item.rules"
              :style="itemStyle"
              :prop="item.field"
            >
              <template
                v-if="
                  item.type === 'input' ||
                  item.type === 'password' ||
                  item.type === 'number'
                "
              >
                <el-input
                  :placeholder="item.placeholder"
                  :show-password="item.type === 'password'"
                  :value="formData[`${item.field}`]"
                  v-bind="item.otherOptions"
                  @input="handleValueChange($event, item.field)"
                >
                  <template v-if="item.slot">
                    <template slot="append">
                      {{ item.slot.append }}
                    </template>
                  </template>
                </el-input>
              </template>

              <template v-else-if="item.type === 'select'">
                <el-select
                  :placeholder="item.placeholder"
                  style="width: 100%"
                  v-bind="item.otherOptions"
                  :value="formData[`${item.field}`]"
                  clearable
                  @input="handleValueChange($event, item.field)"
                >
                  <el-option
                    v-for="option in item.options"
                    :key="option.value"
                    :value="option.value"
                    :label="option.label"
                  />
                </el-select>
              </template>

              <template v-else-if="item.type === 'datepicker'">
                <el-date-picker
                  style="width: 100%"
                  v-bind="item.otherOptions"
                  :value="formData[`${item.field}`]"
                  @input="handleValueChange($event, item.field)"
                />
              </template>
              <template v-else-if="item.type === 'inputNumber'">
                <el-input-number
                  :value="formData[`${item.field}`]"
                  v-bind="item.otherOptions"
                  controls-position="right"
                  :placeholder="item.placeholder"
                  @input="handleValueChange($event, item.field)"
                >
                </el-input-number>
              </template>
            </el-form-item>
          </el-col>
        </template>
        <slot name="btns" />
      </el-row>
    </el-form>
    <div class="footer">
      <slot name="footer" />
    </div>
  </div>
</template>

<script>
export default {
  props: {
    callBack: {
      type: Function,
      default: () => {},
    },
    formData: {
      type: Object,
      required: true,
    },
    formItems: {
      type: Array,
      default: () => [],
    },
    labelWidth: {
      type: String,
      default: "100px",
    },
    itemStyle: {
      type: Object,
      default: () => ({ padding: "0px 0px" }),
    },
    colLayout: {
      type: Object,
      default: () => ({
        // xl: 6,
        // lg: 8,
        xl: 8,
        lg: 8,
        md: 12,
        sm: 24,
        xs: 24,
      }),
    },
    advanced: {
      type: Boolean,
      default: false,
    },
    defaultItemsNum: {
      type: Number,
      default: 2,
    },
    alwaysShow: {
      type: Boolean,
      default: false,
    },
  },
  methods: {
    resetForm() {
      this.$refs.elForm.resetFields();
    },
    handleValueChange(value, field) {
      this.$emit("updateFormData", { value, field });
    },
    formValidate(callBack) {
      this.$refs.elForm.validate((valid) => {
        if (valid) {
          callBack(valid);
        }
      });
    },
  },
};
</script>

<style lang="scss" scoped></style>
